﻿using System;
using System.IO;
using System.Security.Cryptography;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.UIElements;
using ZyGame.Editor.AssetBuilder;

namespace ZyGame.Editor
{
    public class ZyGameOptions : SettingsProvider
    {
        static ZyGameOptions provider;
        public static ZyGameOptions instance => provider;

        SerializedObject serializedObject;
        SerializedObject configObject;

        public ZyGameOptions() : base("Project/Options", SettingsScope.Project)
        {
        }

        public override void OnGUI(string searchContext)
        {
            if (configObject is null)
            {
                configObject = new SerializedObject(AppConfig.instance);
                serializedObject = new SerializedObject(AssetBuilderSetting.instance);
            }

            if (AppConfig.instance.vfsOptions.SecretId.IsNullOrEmpty() || AppConfig.instance.vfsOptions.SecretKey.IsNullOrEmpty())
            {
                RijndaelManaged rijma = new RijndaelManaged();
                rijma.GenerateKey();
                rijma.GenerateIV();
                AppConfig.instance.vfsOptions.SecretId = Convert.ToBase64String(rijma.Key);
                AppConfig.instance.vfsOptions.SecretKey = Convert.ToBase64String(rijma.IV);
                Debug.Log(AppConfig.instance.vfsOptions.SecretId + " " + AppConfig.instance.vfsOptions.SecretKey);
                AppConfig.Save();
                this.Repaint();
                return;
            }

            EditorGUI.BeginChangeCheck();

            GUILayout.BeginVertical("App Options", EditorStyles.helpBox);
            GUILayout.Space(20);
            SerializedProperty sp = configObject.FindProperty("gameOptions");
            if (sp is not null)
            {
                EditorGUILayout.PropertyField(sp, new GUIContent("Game"), true);
            }

            sp = configObject.FindProperty("vfsOptions");
            if (sp is not null)
            {
                EditorGUILayout.PropertyField(sp, new GUIContent("VFS"), true);
            }

            sp = configObject.FindProperty("addressOptions");
            if (sp is not null)
            {
                EditorGUILayout.PropertyField(sp, new GUIContent("VFS"), true);
            }

            sp = configObject.FindProperty("hotfixOptions");
            if (sp is not null)
            {
                EditorGUILayout.PropertyField(sp, new GUIContent("VFS"), true);
            }

            GUILayout.EndVertical();

            GUILayout.Space(20);

            GUILayout.BeginVertical("Builder Options", EditorStyles.helpBox);
            GUILayout.Space(20);
            sp = serializedObject.FindProperty("options");
            if (sp is not null)
            {
                EditorGUILayout.PropertyField(sp, new GUIContent("Builder Default Options"), true);
            }

            sp = serializedObject.FindProperty("ConfigOptions");
            if (sp is not null)
            {
                EditorGUILayout.PropertyField(sp, new GUIContent("Import Config Options"), true);
            }

            sp = serializedObject.FindProperty("uploads");
            if (sp is not null)
            {
                EditorGUILayout.PropertyField(sp, new GUIContent("Builder Upload Options"), true);
            }

            GUILayout.EndVertical();


            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
                configObject.ApplyModifiedProperties();
                AssetBuilderSetting.Save();
                AppConfig.Save();
            }
        }

        [SettingsProvider]
        public static SettingsProvider CreateZyGameOptions()
        {
            if (provider == null)
            {
                provider = new ZyGameOptions();
            }

            return provider;
        }
    }
}